<?php

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\brand\Brand;
use Biz\model\brandAccount\BrandAccountAuth;
use Biz\model\brandAccount\BrandAccountInfo;
use Biz\tools\Mail;

$mail = $argv[1] ?? '';

$accountList = BrandAccountInfo::getAllByWhere(['account_status' => BrandAccountInfo::ACCOUNT_STATUS_EXPIRE, 'type' => BrandAccountInfo::ACCOUNT_TYPE_BRAND]);
$accountIds = array_column($accountList, 'id');
$brandIds = array_column($accountList, 'brand_id');
$brandInfos = Brand::selectByWhere(['id' => $brandIds], ['id', 'brand_name']);
$brandInfos = array_column($brandInfos, null, 'id');
$list = [];
foreach ($accountList as $row) {
    $brandInfo = ($row['brand_id'] && isset($brandInfos[$row['brand_id']])) ? $brandInfos[$row['brand_id']] : [];
    $info = [
        'id' => $row['id'],
        'uid' => $row['uid'],
        'nick' => $row['nick'],
        'brand_id' => $row['brand_id'],
        'brand_name' => $brandInfo['brand_name'] ?? '',
        'account_status' => $row['account_status'],
        'auth_status' => $row['auth_status'],
        'in_start_time' => $row['in_start_time'],
        'in_end_time' => $row['in_end_time'],
        'name' => '',
        'phone' => '',
        'email' => '',
        'weixin' => '',
    ];
    $authList = BrandAccountAuth::getAllByWhere([
        'account_id' => $row['id'],
        'auth_status[!]' => BrandAccountAuth::AUTH_STATUS_DEL,
        'ORDER' => ['id' => 'DESC'],
        'LIMIT' => [0, 3]
    ], ['id', 'account_id', 'name', 'phone', 'email', 'weixin']);
    foreach ($authList as $row) {
        if (empty($info['name'])) {
            $info['name'] = $row['name'];
        }
        if (empty($info['phone'])) {
            $info['phone'] = $row['phone'];
        }
        if (empty($info['email'])) {
            $info['email'] = $row['email'];
        }
        if (empty($info['weixin'])) {
            $info['weixin'] = $row['weixin'];
        }
    }
    $list[] = $info;
}

$sheetData = [
    ['cell_title' => '账户ID', 'cell_key' => 'A', 'cell_width' => 15, 'value_key' => 'id'],
    ['cell_title' => 'uid', 'cell_key' => 'B', 'cell_width' => 15, 'value_key' => 'uid'],
    ['cell_title' => '昵称', 'cell_key' => 'C', 'cell_width' => 25, 'value_key' => 'nick'],
    ['cell_title' => '品牌id', 'cell_key' => 'D', 'cell_width' => 15, 'value_key' => 'brand_id'],
    ['cell_title' => '品牌名称', 'cell_key' => 'E', 'cell_width' => 25, 'value_key' => 'brand_name'],
    ['cell_title' => '账户状态', 'cell_key' => 'F', 'cell_width' => 15, 'value_key' => 'account_status'],
    ['cell_title' => '认证状态', 'cell_key' => 'G', 'cell_width' => 15, 'value_key' => 'auth_status'],
    ['cell_title' => '入驻开始时间', 'cell_key' => 'H', 'cell_width' => 20, 'value_key' => 'in_start_time'],
    ['cell_title' => '入驻结束时间', 'cell_key' => 'I', 'cell_width' => 20, 'value_key' => 'in_end_time'],
    ['cell_title' => '姓名', 'cell_key' => 'J', 'cell_width' => 20, 'value_key' => 'name'],
    ['cell_title' => '电话', 'cell_key' => 'K', 'cell_width' => 25, 'value_key' => 'phone'],
    ['cell_title' => '邮箱', 'cell_key' => 'L', 'cell_width' => 15, 'value_key' => 'email'],
    ['cell_title' => '微信', 'cell_key' => 'M', 'cell_width' => 15, 'value_key' => 'weixin'],
];


$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
//设置表头
foreach ($sheetData as $row) {
    $sheet->setCellValue($row['cell_key'] . '1', $row['cell_title']);
    $sheet->getColumnDimension($row['cell_key'])->setWidth($row['cell_width']);
}
//设置表格数据
$rowNum = 2; // 从第二行开始
foreach ($list as $info) {
    foreach ($sheetData as $row) {
        $key = $row['value_key'];
        $value = isset($info[$key]) ? $info[$key] : '';
        switch ($key) {
            case 'account_status':
                $value = BrandAccountInfo::ACCOUNT_STATUS_MAP[$value];
                break;
            case'auth_status':
                $value = BrandAccountInfo::AUTH_STATUS_MAP[$value];
                break;
            case 'in_start_time':
                if ($value == '1970-01-01') {
                    $value = '';
                }
                break;
            case 'in_end_time':
                if ($value == '1970-01-01') {
                    $value = '';
                }
        }
        $sheet->setCellValue($row['cell_key'] . $rowNum, $value);
    }
    $rowNum++;
}
$fileName = '品牌账户信息' . date('YmdHis') . '.xlsx';
$filePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $fileName;
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $fileName . '"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.0
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save($filePath);
$adminEmail = ['tangyiwan@haohaozhu.com'];
if ($mail) {
    $adminEmail[] = $mail;
}
Mail::send($adminEmail, '品牌账户信息', '导出日期:' . date("Y年m月d日 H:i:s"), $filePath);

